﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GandAInvitaciones.Base;
using GandAInvitaciones.CommonServiceReference;

namespace GandAInvitaciones
{
    public partial class SiteGanda : System.Web.UI.MasterPage
    {
        #region Propiedades
        public string TituloPagina
        {
            get { return LblTituloPagina.Text; }
            set { LblTituloPagina.Text = value; }
        }

        public bool showLblmv
        {
            set { lblmv.Visible = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            bool esMobile = false;
            string strUserAgent = Request.UserAgent.ToString().ToLower();

            if (!Page.IsPostBack)
            {
                if (strUserAgent != null)
                {
                    if (Request.Browser.IsMobileDevice == true || strUserAgent.Contains("iphone") ||
                    strUserAgent.Contains("blackberry") || strUserAgent.Contains("mobile") ||
                    strUserAgent.Contains("windows ce") || strUserAgent.Contains("opera mini") ||
                    strUserAgent.Contains("palm"))
                    {
                        esMobile = true;
                        pnlSitioMovil.Visible = true;
                    }
                    else
                    {
                        pnlSitioMovil.Visible = false;
                    }
                }

                if (Session["AplicaVisita"] != null)
                {
                    try
                    {
                        ////string SourceXML = @"http://api.hostip.info/?ip=" + PageBase.GetDireccionIp(Request);
                        string SourceXML = @"http://api.ipinfodb.com/v3/ip-city/?key=f05d985c185ef43779b78354a2e4bc03c9fa8104c04a457f7e91028f56efe0e5&ip=" + PageBase.GetDireccionIp(Request) + "&format=xml";
                        DataSet ds = new DataSet();
                        ds.ReadXml(SourceXML);
                        //DataTable dt = ds.Tables["Hostip"];
                        DataTable dt = ds.Tables["Response"];
                        
                        CommonServiceReference.Visita obj = new CommonServiceReference.Visita();
                        obj.IpVista = PageBase.GetDireccionIp(Request);
                        obj.FechaVisita = DateTime.Now;
                        obj.Explorador = strUserAgent;
                        obj.SistemaOperativo = Request.Browser.Platform;
                        obj.ReferenciaUrl = PageBase.GetReferencesUrl(Request);
                        obj.EsMobile = esMobile;
                        if (dt.Rows.Count > 0)
                        {
                            obj.Ciudad = dt.Rows[0]["cityName"].ToString();
                            obj.Estado = dt.Rows[0]["regionName"].ToString();
                            obj.Pais = dt.Rows[0]["countryName"].ToString();
                            obj.Latitud = dt.Rows[0]["latitude"].ToString();
                            obj.Longitud = dt.Rows[0]["longitude"].ToString();
                        }

                        CommonServiceReference.CommonServiceClient srv = new CommonServiceReference.CommonServiceClient();
                        srv.GuardaVisita(obj);
                    }
                    catch (Exception ex)
                    {
                        GandA.Component.Log.Log.WriteLog(ex, "Error en logue de Visita");
                    }
                    finally
                    {
                        Session.Remove("AplicaVisita");
                    }
                }

                if (Session["VerEscritorio"] == null)
                {
                    Session["VerEscritorio"] = Request["verEscritorio"] == null ? false : (Request["verEscritorio"] == "1" ? true : false);
                    if (esMobile && !Convert.ToBoolean(Session["VerEscritorio"]))
                        Response.Redirect("http://m.ganda-invitaciones.com");
                }
            }
        }

        protected void BntEnviarReg_Click(object sender, EventArgs e)
        {
            try
            {
                CommonServiceReference.CommonServiceClient srv = new CommonServiceClient();
                CommonServiceReference.Boletin obj = new CommonServiceReference.Boletin();
                obj.CorreoElectronico = txtCorreo.Text.Trim();
                obj.IpMod = PageBase.GetDireccionIp(Request);
                srv.GuardaBoletin(obj);
                alert("Gracias por inscribirte en nuestro boletín");
                txtCorreo.Text = "";
                updTxtCorreo.Update();
            }
            catch (Exception ex)
            {
                alert(ex.Message);
            }
        }

        #region Metodos
        public void alert(string msg)
        {
            if (Page.IsPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "alert", "alert('" + msg + "');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "alert", "alert('" + msg + "');", true);
            }
        }

        public void alert(string msg, string codigoExtra)
        {
            if (Page.IsPostBack)
            {
                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "alert", "alert('" + msg + "');" + codigoExtra, true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "alert", "alert('" + msg + "');" + codigoExtra, true);
            }
        }

        public void alertExito()
        {
            alert("Operación exitosa");
        }

        public void alertExito(string codigoExtra)
        {
            alert("Operación exitosa", codigoExtra);
        }

        public void alertSinDatos()
        {
            alert("No se encontraron resultados");
        }

        public void alertSinDatos(string codigoExtra)
        {
            alert("No se encontraron resultados", codigoExtra);
        }

        public void DisabledBoletin()
        {
            txtCorreo.EnabledValidator = false;
        }
        #endregion
    }
}